package com.yitiao.base.diff;

import org.apache.commons.lang3.builder.Diff;
import org.apache.commons.lang3.builder.DiffBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.ArrayList;
import java.util.List;

public class DiffMain {
    public static void main(String[] args) {
        Address address1 = new Address("Street 1", "City 1");
        Address address2 = new Address("Street 2", "City 2");

        List<String> hobbies1 = new ArrayList<>();
        hobbies1.add("Reading");
        hobbies1.add("Gardening");

        List<String> hobbies2 = new ArrayList<>();
        hobbies2.add("Reading");
        hobbies2.add("Cooking");

        Person person1 = new Person("John", 25, address1, hobbies1);
        Person person2 = new Person("John", 30, address2, hobbies2);

        DiffBuilder diffBuilder = new DiffBuilder(person1, person2, ToStringStyle.SHORT_PREFIX_STYLE);
        diffBuilder.append("名称", person1.getName(), person2.getName())
                .append("年龄", person1.getAge(), person2.getAge())
                .append("地址", person1.getAddress(), person2.getAddress())
                .append("爱好", person1.getHobbies(), person2.getHobbies());

        List<Diff<?>> diffs = diffBuilder.build().getDiffs();
        diffs.forEach(d->{
            System.out.println(d.getFieldName());
            System.out.println(d.getRight()+":"+d.getLeft());
//            年龄
//            30:25
//            地址
//            Address(street=Street 2, city=City 2):Address(street=Street 1, city=City 1)
//            爱好
//            [Reading, Cooking]:[Reading, Gardening]
        });
        System.out.println(diffBuilder.build().getDiffs());
    }
}
